package com.gjt.dlmall.product.app;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gjt.dlmall.product.entity.CategoryEntity;
import com.gjt.dlmall.product.service.CategoryService;
import com.gjt.common.utils.R;



/**
 * 商品类目
 *
 * @author gjt
 * @email g45852139@163.com
 * @date 2021-09-02 23:10:21
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类，以及树形结构组装起来
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")@RequestParam Map<String, Object> params
    public R list(){
        //PageUtils page = categoryService.queryPage(params);
        List<CategoryEntity> entityList = categoryService.listwithTree();
        return R.ok().put("data", entityList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("id") Integer id){
		CategoryEntity category = categoryService.getById(id);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
        categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Integer[] ids){
        //直接删除方法
		//categoryService.removeByIds(Arrays.asList(ids));
        //TODO 检查当前要删除的分类菜单 是否被别的引用
        //逻辑删除
        categoryService.removeMenuByIds(Arrays.asList(ids));
        return R.ok();
    }

}
